Skip to contentMethod: static {...}
1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * SteelBlue: DCI User Interfaces
5: * http://tidalwave.it/projects/steelblue
6: *
7: * Copyright (C) 2015 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *************************************************************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/steelblue-src
22: * git clone https://github.com/tidalwave-it/steelblue-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.ui.core.role;
27:
28: import jakarta.annotation.Nonnull;
29: import java.util.Collection;
30: import java.util.List;
31: import java.util.Optional;
32: import it.tidalwave.util.NotFoundException;
33:
34: /***************************************************************************************************************************************************************
35: *
36: * A role that provides {@link UserAction}s.
37: *
38: * @since 2.0-ALPHA-1
39: * @stereotype role
40: *
41: * @author Fabrizio Giudici
42: *
43: **************************************************************************************************************************************************************/
44: public interface UserActionProvider
45: {
46: public static final Class<UserActionProvider> _UserActionProvider_ = UserActionProvider.class;
47:
48: /***********************************************************************************************************************************************************
49: * Returns a collection of {@link UserAction}s.
50: *
51: * @return a collection of actions
52: **********************************************************************************************************************************************************/
53: @Nonnull
54: public Collection<? extends UserAction> getActions();
55:
56: /***********************************************************************************************************************************************************
57: * Returns the default action, if available.
58: *
59: *
60: * @return the default action
61: * @throws NotFoundException if there's no default action
62: * @deprecated Use {@link #getOptionalDefaultAction()}
63: **********************************************************************************************************************************************************/
64: @Nonnull @Deprecated
65: public UserAction getDefaultAction()
66: throws NotFoundException;
67:
68: /***********************************************************************************************************************************************************
69: * Returns the default action, if available.
70: *
71: * @since 3.1-ALPHA-2
72: * @return the default action
73: **********************************************************************************************************************************************************/
74: @Nonnull
75: public default Optional<UserAction> getOptionalDefaultAction()
76: {
77: try
78: {
79: return Optional.of(getDefaultAction());
80: }
81: catch (NotFoundException e)
82: {
83: return Optional.empty();
84: }
85: }
86:
87: /***********************************************************************************************************************************************************
88: * Factory method which creates an instance out of an array of {@link UserAction}s. The first one is considered the
89: * default action.
90: *
91: * @since 3.1-ALPHA-2
92: * @param actions the actions
93: * @return the {@code UserActionProvider}
94: **********************************************************************************************************************************************************/
95: @Nonnull
96: public static UserActionProvider of (@Nonnull final UserAction ... actions)
97: {
98: return new UserActionProvider()
99: {
100: @Override @Nonnull
101: public Collection<? extends UserAction> getActions()
102: {
103: return List.of(actions);
104: }
105:
106: @Override @Nonnull
107: public UserAction getDefaultAction()
108: throws NotFoundException
109: {
110: if (actions.length == 0)
111: {
112: throw new NotFoundException();
113: }
114:
115: return actions[0];
116: }
117: };
118: }
119: }